package com.yes_iot.controller;

import com.yes_iot.entity.LargeScreenConfiguration;
import com.yes_iot.entity.VisualDisplay;
import com.yes_iot.entity.VisualMoudle;
import com.yes_iot.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/yesiot")
@Slf4j
@CrossOrigin
public class LargeScreenConfigurationController extends BaseController{
    @RequestMapping(value = "/largeScreenConfiguration", method = RequestMethod.GET)
    public Result getOne(@RequestParam String largeScreenConfigurationId)  {
        try {
            checkNotNull("largeScreenConfigurationId",largeScreenConfigurationId);
            LargeScreenConfiguration largeScreenConfiguration=largeScreenConfigurationService.findById(UUID.fromString(largeScreenConfigurationId));
            if (largeScreenConfiguration!=null){
                return Result.succeed(largeScreenConfiguration,"查询成功");
            }else{
                return Result.failed("传值错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    @RequestMapping(value = "/largeScreenConfiguration/farmId", method = RequestMethod.POST)
    public Result findByIds(@RequestBody List<Integer> farmIds)  {
        try {
            checkNotNull("farmIds",farmIds);

            List<LargeScreenConfiguration> largeScreenConfigurations=largeScreenConfigurationService.findByFarmIds(farmIds);
            if (largeScreenConfigurations!=null){
                return Result.succeed(largeScreenConfigurations,"查询成功");
            }else{
                return Result.failed("传值错误");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }
    @RequestMapping(value = "/largeScreenConfiguration/farmId/{farmId}", method = RequestMethod.GET)
    public Result existFarmId(@PathVariable Integer farmId)  {
        try {
            List<Integer> farmIds = new ArrayList<>();
            farmIds.add(farmId);

            List<LargeScreenConfiguration> byFarmIds = largeScreenConfigurationService.findByFarmIds(farmIds);
            if (byFarmIds!=null&& !byFarmIds.isEmpty()){
                return Result.succeed("查询成功");
            }else{
                return Result.failed("查询失败 ");
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }


    @RequestMapping(value = "/largeScreenConfiguration/save", method = RequestMethod.POST)
    public Result save(@RequestBody LargeScreenConfiguration largeScreenConfiguration)  {
        try {
            if (largeScreenConfiguration.getId()==null){
                largeScreenConfiguration.setId(UUID.randomUUID());
            }
            largeScreenConfigurationService.save(largeScreenConfiguration);
            return Result.succeed(null,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

    @RequestMapping(value = "/largeScreenConfiguration/basic/update", method = RequestMethod.POST)
    public Result basicUpdate(@RequestBody LargeScreenConfiguration largeScreenConfiguration)  {
        try {
            if (largeScreenConfiguration.getId()==null){
                largeScreenConfiguration.setId(UUID.randomUUID());
            }
            largeScreenConfigurationService.basicUpdate(largeScreenConfiguration);
            return Result.succeed(null,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("当前地块已创建大屏组态！");
        }
    }
    @RequestMapping(value = "/largeScreenConfiguration/visual/update", method = RequestMethod.POST)
    public Result visualUpdate(@RequestBody LargeScreenConfiguration largeScreenConfiguration)  {
        try {
            if (largeScreenConfiguration.getId()==null){
                largeScreenConfiguration.setId(UUID.randomUUID());
            }
            largeScreenConfigurationService.visualUpdate(largeScreenConfiguration);
            return Result.succeed(null,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }
    @RequestMapping(value = "/largeScreenConfiguration/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id)  {
        try {
            largeScreenConfigurationService.del(id);
            return Result.succeed("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }
    }

}
